<?php
/*!
IndexController
EXTENDS Zend_Controller_Action

*/
class IndexController extends Zend_Controller_Action
{

    public function init()
    {
		//! Initializes models for IP address, building, model, serial number, comments, and component.\n
		//! Initializes redirector for page redirects.\n
		//! Creates/accesses 'post' namespace.\n
		
        $this->ipModel = new Application_Model_IP();
		$this->buildingModel = new Application_Model_Building();
		$this->modelModel = new Application_Model_Model();
		$this->serialnumberModel = new Application_Model_SerialNumber();
		$this->commentsModel = new Application_Model_Comments();
		$this->componentModel = new Application_Model_Component();
		$this->post = new Zend_Session_Namespace('post');
		
		$this->_redirector = $this->_helper->getHelper('Redirector');
    }

    public function indexAction()
    {
		//! Gets array of building names.\n
		//! Creates/accesses 'search' namespace.\n
		//! Returns /index action.\n
		
		$this->timestamp();
		$buildings = $this->buildingModel->get_all_buildings();
		$this->view->buildings = $buildings;
		
		$search = new Zend_Session_Namespace('search');
		$search->back_to_results = true;
		$this->post->data = null;
    }

    public function searchbybuildingAction()
    {	
		//! Calls building model.\n
		//! Creates/accesses 'building' namespace.\n
		//! Queries POST data with building model.\n
		//! Stores query results in 'building' namespace.\n
		//! Stores search method in 'search' namespace.\n
		//! Returns /index/searchbybuilding action.\n
		//! Returns /index/viewdetails action IF query results == 1.\n
		
		$this->timestamp();
		if ($this->post->data == null)
			$this->post->data = $_POST;
			
        $building = $this->post->data['building'];
		
		$buildingResults = new Zend_Session_Namespace('building');
		$buildingResults->name = $building;

		$search = new Zend_Session_Namespace('search');
		$search->search_method = 'searchbybuilding';
		
		$result = $this->buildingModel->search_by_building($building);
		$buildingResults->results = $result;
		if(count($result)==1){
			$search->back_to_results = false;
			$component_id=$result[0]['component_id'];
			$this->_redirector->gotoUrl('/index/viewdetails?id='.$component_id);
		}
    }

    public function searchbyfieldAction()
    {
		//! Calls model, serial number, and comments model.\n
		//! Creates/accesses 'field' namespace.\n
		//! Queries POST data with appropriate model.\n
		//! Stores query results in 'field' namespace.\n
		//! Stores search method in 'search' namespace.\n
		//! Returns /index/searchbyfield action.\n
		//! Returns /index/viewdetails action IF query results == 1.\n
		
		$this->timestamp();
		if ($this->post->data == null)
			$this->post->data = $_POST;
			
		$field = $this->post->data['field'];
		$criteria = $this->post->data['criteria'];
		
		$fieldResults = new Zend_Session_Namespace('field');
		
		$search = new Zend_Session_Namespace('search');
		$search->search_method = 'searchbyfield';
		
		if ($field == 'Model')			
			$result = $this->modelModel->search_by_model($criteria);	
		else if ($field == 'Serial Number')
			$result = $this->serialnumberModel->search_by_serialnumber($criteria);
		else if ($field == 'Comments')			
			$result = $this->commentsModel->search_by_comments($criteria);

		$fieldResults->results = $result; 
		$this->view->message = $field;
		if(count($result)==1){
			$search->back_to_results = false;
			$component_id=$result[0]['component_id'];
			$this->_redirector->gotoUrl('/index/viewdetails?id='.$component_id);
		}		
    }

    public function searchbyipAction()
    {
		//! Calls ip model.\n
		//! Creates/accesses 'ip_address' namespace.\n
		//! Queries POST data with ip model.\n
		//! Stores query results in 'ip_address' namespace.\n
		//! Stores search method in 'search' namespace.\n
		//! Returns /index/searchbyip action.\n
		//! Returns /index/viewdetails action IF query results == 1.\n
		
		$this->timestamp();
		if ($this->post->data == null)
			$this->post->data = $_POST;
			
		$ip_address = $this->post->data['ip_address'];
		
		$ipResults = new Zend_Session_Namespace('ip_address');
		
		$search = new Zend_Session_Namespace('search');
		$search->search_method = 'searchbyip';
		
		$result = $this->ipModel->search_by_ip($ip_address);		
		$ipResults->results = $result;
		
		if(count($result)==1){
			$search->back_to_results = false;
			$component_id=$result[0]['component_id'];
			$this->_redirector->gotoUrl('/index/viewdetails?id='.$component_id);
		}
    }

    public function viewdetailsAction()
    {
		//! Calls component detail and contact model.\n
		//! Creates/accesses 'contacts' namespace.\n
		//! Creates/accesses 'details' namespace.\n
		//! Queries POST data with component detail model.\n
		//! Stores query results in 'details' namespace.\n
		//! Returns /index/viewdetails action.\n
		
		$this->timestamp();
		$id=$_GET["id"];
        
		$detailsModel = new Application_Model_ComponentDetail();
		$contactModel = new Application_Model_Contact();
		
		$contacts = $contactModel->get_all_contacts();
				
		$contactNamespace=new Zend_Session_Namespace('contacts');
		$contactNamespace->contacts=$contacts;

		$results = $detailsModel->get_details($id);
		
		$detailResults = new Zend_Session_Namespace('details');
		$detailResults->results=$results;
    }
	
	public function returnAction()
	{
		//! Creates/accesses 'search' namespace.\n
		//! Returns /index/search_method action stored in 'search' namespace.\n
		
		$this->timestamp();
		$search = new Zend_Session_Namespace('search');
		$search_method = $search->search_method;
		
		$this->_redirector->gotoUrl('/index/'.$search_method);
	}
	
	public function updatecomponentAction() 
	{
		//! Calls component model.\n
		//! Queries POST data with component model.\n
		//! Returns /index/viewdetails action.\n
		
		$this->timestamp();
		$component_id = $_POST['component_id'];
		$ip_address = $_POST['ip_address'];
		$location = $_POST['location'];
		$last_inspected = $_POST['last_inspected'];
		$component_comments = $_POST['component_comments'];
		$contact_id=$_POST['contact_id'];
		$serial_number=$_POST['serial_number'];

		$componentModel = $this->componentModel;
		
		$componentModel->update_component( 
			$component_id, 
			$ip_address, 
			$location, 
			$last_inspected,
			$component_comments,
			$contact_id,
			$serial_number
		);
		$this->_redirector->gotoUrl('/index/viewdetails?id='.$component_id);
	}
	
	public function timestamp() {
		//! Creates/accesses 'login_session' namespace.\n
		//! Regenerates session ID.\n
		//! Returns /login action IF 'login_session' end_seconds is exceeded.\n
		
		$login_session = new Zend_Session_Namespace('login_session');
		
		if (($login_session->start_time != "") && (time() - $login_session->start_time > $login_session->end_seconds)) {
			// login was more than 15 minutes ago
			session_destroy(); // destroy session data in storage
			session_unset(); // unset $_SESSION variable for the runtime
			$this->_redirector->gotoUrl('/login');
		}
		
		if (($login_session->check_time != "") && (time() - $login_session->check_time > 30)) {
			// session started more than 5 minutes ago
			session_regenerate_id(true);    // change session ID for the current session an invalidate old session ID
			$login_session->check_time = time();  // update creation time
		}
	}
}





